<?php
    function RoundIdsInOrder($seq_code = "default") {
        global $dpdb;
        static $_ids;
        if(!isset($_ids)) {
            $sql = "
                SELECT roundid
                FROM project_round_sequence
                WHERE sequence_code = '{$seq_code}'
                ORDER BY sequence";
            $_ids = $dpdb->SqlValues($sql);
        }
        return $_ids;
    }

    function RoundIndexForId($roundid) {
        if(strtoupper($roundid) == "OCR") {
            return 0;
        }
        $i = 1;
        foreach(RoundIdsInOrder() as $id) {
            if($id == $roundid) {
                return $i;
            }
            $i++;
        }
        return null;
    }

    function RoundIdBefore($roundid) {
        $index = RoundIndexForId($roundid);
        if($index == 0)
            return null;
        return RoundIdForIndex($index - 1);
    }

    function RoundIdAfter($roundid) {
        $index = RoundIndexForId($roundid);
        if($index >= count(RoundIdsInOrder()))
            return null;
        return RoundIdForIndex(RoundIndexForId($roundid) + 1);
    }

    function RoundIdForIndex($index) {
        $ary = RoundIdsInOrder();
        return $index >= 1 && $index <= count($ary)
            ? $ary[$index-1]
            : null;
    }

    function UserFieldForRoundIndex($index) {
        if($index < 1 || $index > count(RoundIdsInOrder()))
            return "";
        return sprintf("%s%d%s", "round", $index, "_user");
    }

    function TimeFieldForRoundIndex($index) {
        return sprintf("%s%d%s", "round", $index, "_time");
    }

    function TextFieldForRoundIndex($index) {
        if($index == 0)
            return "master_text";
        else
            return sprintf("%s%d%s", "round", $index, "_text");
    }

    function TextFieldForRoundId($roundid) {
        return TextFieldForRoundIndex(RoundIndexForId($roundid));
    }

    function PreviousUserFieldForRoundid($roundid) {
        $idx = RoundIndexForId($roundid) - 1;
        if($idx < 1)
            return null;
        return UserFieldForRoundId($idx);
    }

    function UserFieldForRoundId($roundid) {
        return UserFieldForRoundIndex(RoundIndexForId($roundid));
    }

    function TimeFieldForRoundId($roundid) {
        return TimeFieldForRoundIndex(RoundIndexForId($roundid));
    }


    function FirstRoundId() {
        return RoundIdForIndex(0);
    }

    function PreviousRoundIdForRoundId($roundid) {
            $idx = RoundIndexForId($roundid) - 1;
            if($idx < 1) {
                return null;
            }
            return RoundIdForIndex($idx);
    }

    function NextRoundDescription() {
        global $Context;
        return $Context->RoundIdDescription(NextRoundId());
    }

    function NextRoundInstructions() {
        global $Context;
        return $Context->RoundIdInstructions(NextRoundId());
    }

    function RoundUrl($roundid) {
        global $proof_url;
        return "{$proof_url}/round.php?roundid={$roundid}";
    }
?>
